package com.jamal2362.apktool.fragment.files;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import androidx.appcompat.app.AlertDialog;
import brut.androlib.ApkOptions;
import java.io.File;
import com.jamal2362.apktool.R;
import com.jamal2362.apktool.fragment.FilesFragment;
import com.jamal2362.apktool.task.BuildTask;
import com.jamal2362.apktool.task.SignUtil;
import com.jamal2362.apktool.util.Settings;

public class BuildItem implements Item {
	private final File projectDir;

	public BuildItem(File projectDir) {
		this.projectDir = projectDir;
	}

	public boolean isSubDir(File dir) {
		return dir.getAbsolutePath().startsWith(projectDir.getAbsolutePath());
	}

	@Override
	public void setup(View root, ImageView icon, TextView name, TextView details, final Refreshable refresh) {
		root.setBackgroundColor(Color.TRANSPARENT);
		if (Settings.lightTheme)
			icon.setImageResource(R.drawable.ic_play_light);
		else
			icon.setImageResource(R.drawable.ic_play_dark);
		name.setText(icon.getContext().getString(R.string.compile_project));
		details.setVisibility(View.GONE);
		icon.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View p1) {
					click(p1, refresh);
				}
			});
	}

	@Override
	public boolean click(View view, final Refreshable refresh) {
		refresh.setScrollPosition(projectDir.getAbsolutePath());
		final Context ctx = view.getContext();
		final Runnable build = new Runnable()
		{
			public void run() {
				SignUtil.loadKey(ctx, new SignUtil.LoadKeyCallback(){
						@Override
						public void call(SignUtil signTool) {
							new BuildTask(ctx, refresh, signTool).execute(projectDir);
						}
					});
			}
		};
		if (Settings.getb(ctx, "confirm_build", false)) {
			final LayoutInflater inflater = LayoutInflater.from(ctx);
			final View dview = inflater.inflate(R.layout.compile, null);
			final TextView tv = dview.findViewById(R.id.compileTextView1);
			String useaapt = ctx.getString(R.string.use_aapt2);
			tv.setText(ctx.getString(R.string.confirmb, projectDir.getName()));
			final RadioButton aapt = dview.findViewById(R.id.compileRadioButton1);
			final RadioButton aapt2 = dview.findViewById(R.id.compileRadioButton2);
			aapt.setText(useaapt.replace("aapt2", "aapt"));
			aapt2.setText(useaapt);
			aapt2.setChecked(Settings.useAapt2);
			AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
			builder.setView(dview);
			builder.setNegativeButton(R.string.cancel, null);
			builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener()
				{
					@Override
					public void onClick(DialogInterface p1, int p2) {
						boolean ua2 = aapt2.isChecked();
						ApkOptions o = ApkOptions.INSTANCE;
						if (ua2) {
							o.aaptPath = Settings.aapt2;
							o.aaptVersion = 2;
						} else {
							o.aaptPath = Settings.aapt;
							o.aaptVersion = 1;
						}
						Settings.setb(ctx, "use_aapt2", ua2);
						Settings.useAapt2 = ua2;
						build.run();
					}
				});
			builder.create().show();
		} else {
			build.run();
		}
		return true;
	}

	@Override
	public boolean edit(FilesFragment frag) {
		return false;
	}


	@Override
	public boolean longClick(View view, Refreshable refresh) {
		// TODO: Implement this method
		return false;
	}

	@Override
	public void process(FilesAdapter adapter) {
	}

	@Override
	public int getProperty() {
		return PROPERTY_BUILD;
	}

	@Override
	public int compareTo(Item p1) {
		return PROPERTY_BUILD - p1.getProperty();
	}
}
